#include "lsDispose.h"



void lsDispose(int netfd,MYSQL * mysql,char * username,char *  prev_path){
    char sql[1024] = {0};
    sprintf(sql,"select * from usrfile where username = '%s' and path = '%s';",username,prev_path);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query 1:%s\n",mysql_error(mysql));
        send(netfd,"0",1,MSG_NOSIGNAL); 
        return ;
    }
    MYSQL_RES * table =  mysql_store_result(mysql);
    
     if(mysql_num_rows(table) == 0){
        send(netfd,"0",1,MSG_NOSIGNAL);
        return ;
    }

    MYSQL_ROW row1 = mysql_fetch_row(table);
    
    if( strcmp(row1[5],"d") == 0){
        bzero(sql,sizeof(sql));
        sprintf(sql,"select * from usrfile where parent_id = %s  ;",row1[0]);
        qret = mysql_query(mysql,sql);
        if(qret != 0){
        fprintf(stderr,"mysql_query 2:%s\n",mysql_error(mysql));
        return ;
        }
        MYSQL_RES * table = mysql_store_result(mysql);
        if(mysql_num_rows(table) == 0){
            send(netfd,"0",1,MSG_NOSIGNAL);
            return;
        }
        send(netfd,"1",1,MSG_NOSIGNAL);
        Train train;
        for(unsigned int i = 0; i < mysql_num_rows(table) ; ++i){
            MYSQL_ROW row2 = mysql_fetch_row(table);
            train.length = strlen(row2[1]);
            memcpy(train.data,row2[1],train.length);
            send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
            send(netfd,row2[5],1,MSG_NOSIGNAL);
        }
        train.length = 0;
        send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    }else{
        send(netfd,"0",1,MSG_NOSIGNAL);
    } 
    return ;
}


